package com.dd.cloud.user.service.paynotice.promode.notice;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dd.cloud.common.constants.IntegralConstants;
import com.dd.cloud.common.constants.TemplateConstants;
import com.dd.cloud.common.exception.ZekeException;
import com.dd.cloud.user.entity.doctor.DoctorEquity;
import com.dd.cloud.user.entity.order.OrderPay;
import com.dd.cloud.user.entity.pres.PresPrescription;
import com.dd.cloud.user.entity.product.ProductGiveRule;
import com.dd.cloud.user.mq.MQConfig;
import com.dd.cloud.user.mq.RabbitSend;
import com.dd.cloud.user.service.doctor.IDoctorEquityService;
import com.dd.cloud.user.service.doctor.IDoctorIntegralService;
import com.dd.cloud.user.service.doctor.IDoctorOrderIncomeDetailService;
import com.dd.cloud.user.service.news.INewsInfoService;
import com.dd.cloud.user.service.order.IOrderPayService;
import com.dd.cloud.user.service.paynotice.promode.NoticeService;
import com.dd.cloud.user.service.paynotice.promode.NoticeType;
import com.dd.cloud.user.service.phar.pharservice.PharServiceBeanInterface;
import com.dd.cloud.user.service.phar.pharservice.PharServiceInterface;
import com.dd.cloud.user.service.pres.IPresPrescriptionService;
import com.dd.cloud.user.service.product.IProductGiveRuleService;
import com.zk.pay.constants.CS;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * 支付处方产品回调
 */
@NoticeType(source = CS.NOTICE_SOURCE.PAY, proType = CS.NOTICE_CHANNEL.PRE)
@Slf4j
public class PrePayNoticeService implements NoticeService {
    @Autowired
    private IOrderPayService orderPayService;
    @Autowired
    private PharServiceBeanInterface pharServiceBeanInterface;
    @Autowired
    private IDoctorOrderIncomeDetailService doctorOrderIncomeDetailService;
    @Autowired
    private IPresPrescriptionService presPrescriptionService;
    @Autowired
    private INewsInfoService newsInfoService;
    @Autowired
    private RabbitSend rabbitSend;
    @Autowired
    private IDoctorIntegralService doctorIntegralService;


    @Autowired
    private IDoctorEquityService doctorEquityService;


    @Autowired
    private IProductGiveRuleService productGiveRuleService;


    /**
     * 支付回调处理逻辑
     *
     * @param request
     * @param orderPay 订单对象
     * @param flowNo   流水编号
     */
    @Override
    @Async
    @Transactional(rollbackFor = Exception.class)
    public void notice(HttpServletRequest request, OrderPay orderPay, String flowNo) {
        //异步方法, 处理结果和异常不能影响回调的返回
        PharServiceInterface pharServiceInterface = pharServiceBeanInterface.getPharService(orderPay);
        if (pharServiceInterface == null) {
            //药房处理的service获取失败
            orderPayService.refund(orderPay.getOrderNo(), 0);
            return;
        }
        log.info("处方发送药房日志");
        pharServiceInterface.sendPresToPhar(orderPay);
        log.info("支付成功增加收入明细日志");
        doctorOrderIncomeDetailService.addDoctorOrderIncomeDetail(orderPay);
        log.info("支付成功增加收入消息通知日志");
        newsInfoService.sendOrderPayNotice(orderPay);
        String params = TemplateConstants.TemplateType.SEVEN_TYPE + "-" + orderPay.getOrderNo();
        log.info("支付成功发送模板消息通知,发送参数{}", params);
        rabbitSend.sendMessage(MQConfig.TEMPLATE_EXCHANGE, MQConfig.TEMPLATE_QUEUE, params);
        log.info("支付成功发送问诊系统消息通知,发送参数{}", orderPay.getServerNo());
        presPrescriptionService.consultPresPaySuccess(orderPay.getServerNo());
        log.info("支付发送积分");
        doctorIntegralService.addDoctorIntegral(orderPay);
        log.info("计算订单数量");
        doctorEquityService.saveDoctorEquity(orderPay,1);
    }

}
